package com.apex.crm.controller;

import com.apex.crm.base.BaseController;
import com.apex.crm.base.ResultInfo;
import com.apex.crm.model.ZTreeMode;
import com.apex.crm.service.ModuleService;
import com.apex.crm.vo.Module;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author XiaoYang
 * @version 1.0
 * @program: crm
 * @Classname ModuleController
 * @description TODO
 * @date 2021/5/3 14:57
 */


@Controller
@RequestMapping("/module")
public class ModuleController extends BaseController {

    @Resource
    private ModuleService moduleService;

    @GetMapping("index")
    public String index(){
        return "module/module";
    }



    /**
     * @author XiaoYang
     * @description //TODO 传入id查询角色
     * @date 14:20 2021/5/26 
     *
     * @param roleId
     * @exception
     * @return java.util.List<com.apex.crm.model.ZTreeMode>
     * @version 1.0
     */

    @PostMapping("queryAllModules")
    @ResponseBody
    public List<ZTreeMode> queryAllModules(Integer roleId){
        return moduleService.queryAllModules(roleId);
    }


    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> queryMoudules(){
        return moduleService.queryModules();
    }

    /**
     * @author XiaoYang
     * @description //TODO  添加菜单
     * @date 14:33 2021/5/26
     *
     * @param module
     * @exception
     * @return com.apex.crm.base.ResultInfo
     * @version 1.0
     */
    @PostMapping("save")
    @ResponseBody
    public ResultInfo saveModule(Module module){
        moduleService.saveModule(module);
        return success("菜单添加成功");
    }


    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateModule(Module module){
        moduleService.updateModule(module);
        return success("菜单更新成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteModule(Integer mid){
        moduleService.deleteModule(mid);
        return success("菜单删除成功");
    }

    @GetMapping("addModulePage")
    public String addModulePage(Integer grade, Integer parentId, Model model){
        model.addAttribute("grade",grade);
        model.addAttribute("parentId",parentId);
        return "module/add";
    }

    @GetMapping("updateModulePage")
    public  String updateModulePage(Integer id, Model model){
        model.addAttribute("module",moduleService.selectByPrimaryKey(id));
        return "module/update";
    }



}
